//
//  FTSlideViewController.m
//  FoodMaster
//
//  Created by 梁尚嘉 on 2017/3/23.
//  Copyright © 2017年 stree7. All rights reserved.
//

#import "FTSlideViewController.h"

@interface FTSlideViewController () <UIScrollViewDelegate>
@property (nonatomic, strong) UIScrollView *pageScrollView;
@property (nonatomic, assign) BOOL isDragging;

@property (nonatomic, strong) FTSlideBar *slideBar;

@end

@implementation FTSlideViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.view.backgroundColor = [UIColor whiteColor];
    self.slideBar = [[FTSlideBar alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, FT_SIZE(100))];
    self.slideBar.dataSource = self;
    self.slideBar.delegate = self;
    self.slideBar.backgroundColor = [UIColor colorWithRed:0.99 green:0.99 blue:0.99 alpha:1];
    [self.view addSubview:self.slideBar];

    self.pageScrollView = [[UIScrollView alloc]init];
    [self.view addSubview:self.pageScrollView];
    self.pageScrollView.pagingEnabled = YES;
    self.pageScrollView.delegate = self;
//    self.pageScrollView.bounces = NO;
    self.pageScrollView.showsHorizontalScrollIndicator = NO;

}


- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
    self.slideBar.frame = CGRectMake(0, 0, self.view.bounds.size.width, FT_SIZE(100));
    [self.pageScrollView setFrame:CGRectMake(0,
                                             self.slideBar.frame.size.height,
                                             self.slideBar.frame.size.width,
                                             self.view.bounds.size.height-self.slideBar.frame.size.height)];
}


- (void)setTitles:(NSArray *)titles
{
    _titles = titles;
    [self.slideBar reloadData];
}

- (void)setControllers:(NSArray<UIViewController *> *)controllers
{
    _controllers = controllers;
    [self.pageScrollView setContentSize:CGSizeMake(FT_SCREEN_WIDTH*self.controllers.count, 0)];
    if (_controllers.count) {
        self.selectedIndex = 0;
    }
}

- (void)setSelectedIndex:(NSUInteger)selectedIndex
{
    _selectedIndex = selectedIndex;
    self.slideBar.selectedIndex = selectedIndex;

    UIViewController *controller = self.controllers[self.selectedIndex];
    if (!controller.parentViewController) {
        [self addChildViewController:controller];
        [self.pageScrollView addSubview:controller.view];
        [controller.view setFrame:CGRectMake(_selectedIndex*self.pageScrollView.bounds.size.width, 0, self.pageScrollView.bounds.size.width, self.pageScrollView.bounds.size.height)];
    }
    
}

- (void)switchToPage:(NSUInteger)pageIndex
            animated:(BOOL)animated
{
    self.selectedIndex = pageIndex;
    [self.pageScrollView setContentOffset:CGPointMake(pageIndex*self.view.bounds.size.width, 0) animated:animated];
}

#pragma mark - Scrollview delegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{

    NSUInteger index = round(self.pageScrollView.contentOffset.x/self.pageScrollView.bounds.size.width);
    self.selectedIndex = index;

}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    _isDragging = YES;
}


- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{

    _isDragging = NO;
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    
}

#pragma mark - FTSlideBarDelegate
- (void)didSelectItemAtIndex:(NSUInteger)index
{
    [self switchToPage:index animated:NO];
}

#pragma mark - FTSlideBarDataSource

- (NSUInteger)numOfItem
{
    return self.titles.count;
}

- (NSString *)itemTitleAtIndex:(NSUInteger)index
{
    return self.titles[index];
}

- (UIColor *)activeColor
{
    return [UIColor colorWithRed:0 green:0.77 blue:1 alpha:1];
}

- (UIColor *)normalColor
{
    return [UIColor colorWithRed:0.29 green:0.29 blue:0.29 alpha:1];
}

- (UIFont *)itemFont
{
    return [UIFont systemFontOfSize:15];
}


@end
